{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二值图与连通域检测综合实践：找到图中所有白色的环\n",
    "假设现在我们获得了下面这张图，如何检测到下图中所有白色的环？这个在实际场景中应用非常广泛。比如拍电影的时候可以用白色的环标记贴在目标物体上来追踪目标物体的轨迹。本章节的理论知识有：{[快速标记连通域检测](../)}。略微涉及了{[图像二值化](../../Otsu's_Method_algorithm)}。如果你想从原理上了解如何检测二值图中的连通物体推荐看这篇教程{[快速标记连通域检测](../%E5%A6%82%E4%BD%95%E7%90%86%E8%A7%A3%E4%BA%8C%E5%80%BC%E5%9B%BE%E8%BF%9E%E9%80%9A%E5%9F%9F%E5%BF%AB%E9%80%9F%E6%A0%87%E8%AE%B0%E7%AE%97%E6%B3%95%E5%BF%AB%E9%80%9F%E8%BF%9E%E9%80%9A%E7%89%A9%E4%BD%93%E6%A3%80%E6%B5%8BFast-Connected-Component%E6%95%99%E7%A8%8B%E9%99%84%E5%B8%A6python%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0.ipynb)}\n",
    "![](./circles.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAD8CAYAAACVSwr3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAGKdJREFUeJztnXvsHUd1xz8Hx4+8yA+HhPrVOiluBapak1pxUBqUxqQOboRRFCAUgakiOWpiCZSq4KhSVaQiQf/gUVEFjILqVEBCk6BYEchN81CLCgYnOJCQhjhR2vxqKxZNHB4WkJDTP+5c+/p3X3t3d3bPzJ6PdHV35+7dPXt25rtnZmdmRVVxHMcZ5FVtG+A4jj1cGBzHGcKFwXGcIVwYHMcZwoXB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2nIvtX05ha5DLgnFCRzdoZjjhKG2q5921WJQkO0W6bS8PImCGHsm+jdjc3ZG8LzA/QG2t1LL0o8qqovj7DluJ3h9xeBs5uwE/g08GHglbB+tlE7IcJUCIO03fOx0BBto5iwXUTOAO4EPqSqP+kNVRm96Yi0RuxV1V8D60Vkjt64mzdMsKUVO0XkSuCIqj4kIpcWsKXt63+xqh4SkXOBe0XkvyZsO7OtbUcMpYZoN8xz/Rmo6hheXicispieKHxJVe8KyWbtVdWjwIP06rlzItK/MQ3actzO8PtZwPMNmHcx8HYReQa4jV514tMG7QROngqBntgenwoh2FTp2rctDN8F1oWW3yX0GnH2tGzTQkwOLw+jWG8BHlfVT1q1V0TOCZECInIq8FbgceAB4Ooxdvbtvxq4X0PFOCaqepOqrlbVtfTy4f2q+l5rdkJDUyE02fg0phFlC70W9aeAv27Zlq8Ah4GX6KnstfTqjfcBT4bv5WFbAf4x2P0DYEPDtv4RvXDw+8CB8NlizV7g94HvBTsfBf4mpJ8PfAc4CPwLsDSkLwvrB8Pv57eQDy7lxFMJc3YGmx4Jn8f65abOa+89Hx3HGaLtqoTjOAZxYXAcZwgXBsdxhnBhcBxnCBcGx3GGcGFwHGcIFwbHcYZwYXAcZ4j/B1Hmpr37JqA3AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 热身训练：调用opencv中的库函数识别连通域\n",
    "import cv2\n",
    "circles_img_mat = cv2.imread('circles.jpg',cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "circles_img_mat = cv2.threshold(circles_img_mat,250,255,cv2.THRESH_BINARY)[1]# 因为连通域算法只适用于二值化了图片所以要进行二值化操作\n",
    "ret, labels = cv2.connectedComponents(circles_img_mat)\n",
    "# 用矩形框标记所识别的连通域\n",
    "for i in range(1,ret):\n",
    "    y,x = np.where(labels==i)\n",
    "    labeled_img_mat = cv2.rectangle(circles_img_mat,(x.min(),y.min()),(x.max(),y.max()),65,3)\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(labeled_img_mat)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
